package config

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"os"
	"strings"
)

type DatabaseConfig struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
}

// UserConfig 用户配置
type UserConfig struct {
	AdminName     string `yaml:"admin_name"`
	AdminPassword string `yaml:"admin_password"`
	SorT          string `yaml:"s_or_t"`
}

// CourseConfig 课程配置
type CourseConfig struct {
	Shiping    ShipingConfig `yaml:"shiping"`
	Live       LiveConfig    `yaml:"live"`
	CourseType int           `yaml:"course_type"`
}

// ShipingConfig 视频课程配置
type ShipingConfig struct {
	Url      string `yaml:"url"`
	Kcdm     string `yaml:"kcdm"`
	Jjwikiid string `yaml:"jjwikiid"`
}

// LiveConfig 直播课程配置
type LiveConfig struct {
	Url      string `yaml:"url"`
	Kcdm     string `yaml:"kcdm"`
	Jjwikiid string `yaml:"jjwikiid"`
}

// LogConfig 日志配置
type LogConfig struct {
	Path string `yaml:"path"`
}

// SocketConfig 配置
type SocketConfig struct {
	Ws   string `yaml:"ws"`
	Ping string `yaml:"ping"`
}
type Config struct {
	Database DatabaseConfig `yaml:"database"`
	Log      LogConfig      `yaml:"log"`
	User     UserConfig     `yaml:"user"`
	Course   CourseConfig   `yaml:"course"`
	Socket   SocketConfig   `yaml:"socket"`
}

// LoadConfig 读取并解析 YAML 配置文件
func LoadConfig(filename string) (*Config, error) {
	// 构建配置文件路径
	// 读取 YAML 文件
	data, err := os.ReadFile(filename)
	if err != nil {
		return nil, fmt.Errorf("error reading config file: %w", err)
	}
	// 解析 YAML 文件
	var cfg Config
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("error unmarshalling YAML: %w", err)
	}

	return &cfg, nil
}
func GetSessionId() (string, error) {
	content, err := ioutil.ReadFile("cookies.txt")
	if err != nil {
		fmt.Println("Error reading file:", err)
		return "", err
	}
	// 将内容转换为字符串
	fileContent := string(content)
	// 处理内容
	parts := strings.Split(fileContent, " ")
	if len(parts) < 2 {
		fmt.Println("Invalid content format")
		return "", err
	}
	// 获取 PHPSESSID 值
	phpsessid := strings.TrimSpace(parts[1])
	return phpsessid, nil
}
